/*
 * SUB.c
 *
 *  Created on: Jan 18, 2014
 *      Author: Rohit
 */

//INCLUDES
#include "includes.h"

/*
 * Function: func_SUB
 *
 * Inputs:	 Address_of_source, Address_of_destination
 * Outputs:  Result
 * Description:
 * Operation: (dst) - (src) + (dst) [in detail, (dst) + - (src) + 1 + (dst)]
	Condition Codes: 2: set if result = 0; cleared otherwise
	N: set if result < 0; cleared otherwise
	C: cleared if there was a carry from the most significant
	bit of the result: set otherwise
	V: set if there was arithmetic overflow as a result of the
	operation, that is, if-the operands were of opposite
	signs and the sign of source was the Same as the
	sign of the result; cleared otherwise.
	Subtract the source operand from the destination operand and
	leaves the result at the destination address. The original contents of the
	destination are lost. The contents of the source are not affected.
 */

unsigned short func_SUB(unsigned short address_source, unsigned short address_destination)
{
	unsigned short data_source;
	unsigned short data_destination;
	int result_32_bit;
	unsigned short result;
	unsigned short result_two_complement;

	data_source 	 = read_from_memory(address_source,DATA);
	data_destination = read_from_memory(address_destination,DATA);

	result_two_complement = get_twos_complement(data_source);

	result_32_bit = data_destination + result_two_complement;

	result	= (unsigned short) result_32_bit;

	write_in_memory(address_destination, result,DATA);

	update_Z_flag(result);
	update_N_flag(SUB, result);
	update_V_flag(SUB, data_source, data_destination, result);
	update_C_flag(SUB, data_source, data_destination, result, result_32_bit);

	return result;
}












